package com.woniu.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启 鉴权的注解
public class WebSecurityConfig  extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserDetailService userDetailService;

    @Autowired
    private MyLoginSuccessHandler loginSuccessHandler;
    @Autowired
    private MyLoginFailHandler failHandler;
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //httpBasic的密码是随机生成的  如：87b83571-bda2-48b2-bfc6-2cef3507abad
        //用户名 默认是   user  ，当然我们可以在配置文件去指定用户名和密码
        //格式：  spring.security.user.name  |  spring.secruity.user.password
        //http.httpBasic().and().authorizeRequests().anyRequest().authenticated();

        /**
         * hasAnyAuthority 指的是权限标识符, 是什么标识符 就是什么标识符
         *
         * hasAnyRole:指的不是权限标识符，它指的是角色符，security会在角色的前面  自动加上ROLE_xxx
         */
        //authorizeRequests 代表所有的请求都需要鉴权,但是"/login","login.html"  不需要权限
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and().formLogin()
                //表单提交的地址：
                .loginProcessingUrl("/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .successHandler(loginSuccessHandler)
                .failureHandler(failHandler);
                //登录成功后  默认要去的首演    /*失败的url    必须是 登陆请求loginProcessingUrl +“xxx”*/
                //.defaultSuccessUrl("/home").permitAll().failureUrl("/login/error");

        http.csrf().disable();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //模拟2个用户  一个是  zhangsan  密码是123  角色是user |||| 一个是admin  密码是123  角色 admin
     /*   auth.inMemoryAuthentication()
                .withUser("zhangsan")
                .password(passwordEncoder.encode("123"))
                .roles("user").and()
                .withUser("admin").
                password(passwordEncoder.encode("123"))
                .roles("admin").and()
                .passwordEncoder(passwordEncoder);*/
        auth.userDetailsService(userDetailService).passwordEncoder(passwordEncoder);



    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }


}
